import { getCode } from "../api/login";
import uuid from 'uuid/v4'
// 获取验证码
export const getCaptcha = {
    mounted() {
        if (localStorage.getItem("sid")) {
            this.sid = localStorage.getItem("sid");
        } else {
            this.sid = uuid();
            localStorage.setItem("sid", this.sid);
        }
        this.$store.commit("setSid", this.sid);
        // 获取验证码
        this._getCode(this.sid);
    },
    methods: {
        // 获取验证码
        _getCode() {
            let sid = this.$store.state.sid;
            getCode(sid).then(res => {
                if (res.errno == 0) {
                    this.svg = res.data;
                }
            });
        }
    },
}

// 获取用户信息 cookie
export const getUser = {
    created() {
        let cookies = document.cookie.split("; ");
        for (const key in cookies) {
            const ele = cookies[key].split("=");
            if (ele[0] == "uhead") {
                this.user.uhead = cookies[key].slice(6);
            } else {
                this.user[ele[0]] = ele[1];
            }
        }
        if (this.user.loginStatus) {
            this.loginStatus = true;
        }
    },
}

// export const getUserL = {
//     created() {
//         const userData = window.localStorage
//         for (const key in userData) {
//             if (userData.hasOwnProperty(key)) {
//                 const element = userData[key];
//                 this.user[key] = element
//             }
//         }

//         if (this.user.loginStatus == '1') {
//             this.loginStatus = true;
//         }

//     },
// }
